<?php 

Class indexModel extends appModel
{
    
    /**
     * 确认是否存在用户信息
     * @param $openid
     * @return Ambigous <string, boolean>
     */
    public function checkExistInfo($openid)
    {
        $sql = " SELECT COUNT(*) FROM ".$this->table("wxuser")." WHERE openid='".$openid."' ";
        return $this->getOne($sql);
    }
    
    /**
     * 更新用户信息
     * @param $info
     * @param $openid
     * @return Ambigous <boolean, resource>
     */
    public function updateUser($info, $openid)
    {
        $data = yf_set_update($info);
        $sql = " UPDATE ".$this->table('wxuser')." SET ".$data." WHERE openid='".$openid."' ";
        return $this->query($sql);
    }
    
    /**
     * 获取用户微信信息
     * @param string $field
     * @param string $where
     * @return Ambigous <boolean, multitype:>
     */
    public function getUserInfo($field = '*', $where = '1')
    {
        if(is_array($field))
        {
            $field = implode(", ", $field);
        }
        if(is_array($where))
        {
            $arr = array();
            foreach($where as $k => $v)
            {
                $arr[] = "`".$k."`='".$v."'";
            }
            $where = implode(" AND ", $arr);
        }
        $sql = " SELECT ".$field." FROM ".$this->table('wxuser')." WHERE ".$where;
        return $this->getRow($sql);
    }
    
    /**
     * 插入用户信息
     * @param $info
     * @return Ambigous <boolean, resource>
     */
    public function insertUser($info)
    {
        $data = yf_set_insert($info);
        $sql = " INSERT INTO ".$this->table('wxuser')." ( ".$data['key']." ) VALUES( ".$data['val']." ) ";
        $this->query($sql);
        return $this->insert_id();
    }
    
    /**
     * 获取参与记录
     * @param unknown $wxid
     * @param unknown $openid
     * @param number $status
     * @return Ambigous <boolean, multitype:multitype: >
     */
    public function getUserJoinInfo($wxid, $openid, $status = 0)
    {
        $sql = " SELECT * FROM ".$this->table('gift_join')." WHERE wxid='".$wxid."' AND openid='".$openid."' ORDER BY id ASC LIMIT 1";
        $data = $this->getRow($sql);
        if(!$data)
        {
            $arr = array(
                'wxid' => $wxid,
                'openid' => $openid,
                'allCount' => 0,
                'gameCount' => 0,
                'shareCount' => 0,
                'gameShare' => 0,
                'allScore' => 0,
                'gameScore' => 0,
                'shareScore' => 0,
                'addScore' => 0,
                'noScore' => 0,
                'created' => yf_time()
            );
            $info = yf_set_insert($arr);
            $sql = " INSERT INTO ".$this->table('gift_join')." ( ".$info['key']." ) VALUES( ".$info['val']." ) ";
            $this->query($sql);
            if($status == 0)
            {
                $data = $this->getUserJoinInfo($wxid, $openid, 1);
            }
            else 
            {
                die('System Error!');
            }
        }
        return $data;
    }
    
    /**
     * 获取规定时间内的记录条数
     * @param unknown $wxid
     * @param unknown $openid
     * @param unknown $limit_time
     * @return Ambigous <string, boolean>
     */
    public function getLimitRecode($wxid, $openid, $limit_time)
    {
        $time = yf_time() - $limit_time;
        $sql = " SELECT COUNT(*) FROM ".$this->table('gift_recode')." WHERE wxid='".$wxid."' AND openid='".$openid."' AND created>'".$time."' ";
        return $this->getOne($sql);
    }
    
    /**
     * 刷新游戏次数
     * @param unknown $id
     * @return Ambigous <boolean, resource>
     */
    public function updateJoinRecodeStart($id) 
    {
        $sql = " UPDATE ".$this->table('gift_join')." SET `gameCount`='0', `created`='".yf_time()."' WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
    /**
     * 获取排名信息
     * @param number $limit
     * @return Ambigous <boolean, multitype:multitype: >
     */
    public function getScoreInfo($limit = 0)
    {
        $limitSql = "";
        if($limit)
        {
            $limitSql = " LIMIT ".$limit." ";
        }
        $sql = " SELECT * FROM ".$this->table('gift_join')." WHERE allScore<>'0' ORDER BY allScore DESC ".$limitSql;
        return $this->getAll($sql);
    }
    
    /**
     * 判断是否第一次游戏
     * @param unknown $arr
     * @return Ambigous <string, boolean>
     */
    public function isExistRecode($arr)
    {
        $where = " WHERE 1=1 ";
        if($arr)
        {
            $temp = array();
            foreach($arr as $k => $v)
            {
                $temp[] = "`".$k."`='".$v."'";
            }
            $where .= " AND ".implode(' AND ', $temp);
        }
        $sql = " SELECT COUNT(*) FROM ".$this->table('gift_recode').$where;
        return $this->getOne($sql);
    }
    
    /**
     * 插入新记录
     * @param unknown $info
     * @return Ambigous <boolean, resource>
     */
    public function insertRecode($info)
    {
        $data = yf_set_insert($info);
        $sql = " INSERT INTO ".$this->table('gift_recode')." ( ".$data['key']." ) VALUES( ".$data['val']." ) ";
        return $this->query($sql);
    }
    
    /**
     * 更新游戏记录
     * @param unknown $info
     * @param unknown $where
     * @return Ambigous <boolean, resource>
     */
    public function updateJoinScore($info, $where)
    {
        if(is_array($where))
        {
            $temp = array();
            foreach($where as $k => $v)
            {
                $temp[] = "`".$k."`='".$v."'";
            }
            $where = implode(' AND ', $temp);
        }
        //$data = yf_set_update($info);
        $data = array();
        if($info['allScore'])
        {
            $data[] = "allScore=allScore+".$info['allScore'];
        }
        if($info['gameScore'])
        {
            $data[] = "gameScore=gameScore+".$info['gameScore'];
        }
        if($info['shareScore'])
        {
            $data[] = "shareScore=shareScore+".$info['shareScore'];
        }
        if($info['noScore'])
        {
            $data[] = "noScore=noScore+".$info['noScore'];
        }
        if($info['addScore'])
        {
            $data[] = "addScore=addScore+".$info['addScore'];
        }
        if($info['allCount'])
        {
            $data[] = "allCount=allCount+".$info['allCount'];
        }
        if($info['gameCount'])
        {
            $data[] = "gameCount=gameCount+".$info['gameCount'];
        }
        if($info['shareCount'])
        {
            $data[] = "shareCount=shareCount+".$info['shareCount'];
        }
        if(count($data) == 0)
        {
            return false;
        }
        $data[] = "lastTime='".yf_time()."'";
        $data[] = "lastIp='".hy_getIP()."'";
        $data = implode(", ", $data);
        $sql = " UPDATE ".$this->table('gift_join')." SET ".$data." WHERE ".$where;
        return $this->query($sql);
    }
    
    /**
     * 更新分享次数
     * @param unknown $wxid
     * @param unknown $openid
     * @return Ambigous <boolean, resource>
     */
    public function updateGameShare($count, $max, $wxid, $openid)
    {
        $joinInfo = $this->getUserJoinInfo($wxid, $openid);
        $all = $count + $joinInfo['gameShare'];
        if($joinInfo['gameShare'] < $max)
        {
            if($max < $all)
            {
                $count = $count - ($all - $max);
            }
        }
        else 
        {
            return false;
        }
        
        $sql = " UPDATE ".$this->table('gift_join')." SET gameShare=gameShare+".$count." WHERE wxid='".$wxid."' AND openid='".$openid."' ";
        return $this->query($sql);
    }
    
    /**
     * 获取分享用户的信息
     * @param unknown $id
     * @return Ambigous <boolean, multitype:>
     */
    public function getShareUserJoin($id)
    {
        $sql = " SELECT wxid, openid FROM ".$this->table('gift_join')." WHERE id='".$id."' ";
        return $this->getRow($sql);
    }
    
}